package com.dongdongshop.controller;

import com.dongdongshop.data.Result;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.service.BrandService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("/brandController")
@Controller
public class BrandController {
    @DubboReference
    private BrandService brandService;

    private Logger logger= LoggerFactory.getLogger(BrandController.class);

    @RequestMapping("/toBrand")
    public String toBrand(){
        return "/admin/brand";
    }

    @RequestMapping("/listBrands")
    @ResponseBody
    public Result listBrands(){
        try {
            List<TbBrand> tbBrands = brandService.listBrands();
//            int i = 9/0;
            return Result.OK().setData(tbBrands);
        }catch (Exception e){
            logger.error("品牌列表查询失败:{}",e);
            return Result.ER();
        }
    }
    @RequestMapping("/saveBrand")
    @ResponseBody
    public Result saveBrand(TbBrand brand){
        try {
            brandService.saveBrand(brand);
            return Result.OK();
        }catch (Exception e){
            logger.error("品牌列表新增失败:{}",e);
            return Result.ER();
        }
    }
    @RequestMapping("/selectBrandById")
    @ResponseBody
    public Result selectBrandById(Long id){
        try {
            TbBrand tbBrand = brandService.selectBrandById(id);
            return Result.OK().setData(tbBrand);
        }catch (Exception e){
            logger.error("品牌列表Id查询失败:{}",e);
            return Result.ER();
        }
    }
    @RequestMapping("/updateBrand")
    @ResponseBody
    public Result updateBrand(String name,Long id,String firstChar){
        TbBrand tbBrand = new TbBrand();
        tbBrand.setName(name);
        tbBrand.setId(id);
        tbBrand.setFirstChar(firstChar);
        try {
            brandService.updateBrand(tbBrand);
            return Result.OK();
        }catch (Exception e){
            logger.error("品牌列表修改失败:{}",e);
            return Result.ER();
        }
    }
    @RequestMapping("/delBrand")
    @ResponseBody
    public Result delBrand(@RequestParam(value = "ids") List<Long> ids){
        try {
            brandService.delBrand(ids);
            return Result.OK();
        }catch (Exception e){
            logger.error("品牌删除错误{}",e);
            return Result.ER();
        }
    }
}
